#include "PlayerStateAttacking.h"
#include "Player.h"
#include "EntityManager.h"
#include "ZeroAttack.h"

PlayerStateAttacking::PlayerStateAttacking(Player* player, sf::Sound* swing01SFX, sf::Sound* swing02SFX, sf::Sound* swing03SFX, EntityManager* em) : 
PlayerState(player),
_swing01SFX(swing01SFX),
_swing02SFX(swing02SFX),
_swing03SFX(swing03SFX),
_entityManager(em),
_za(nullptr)
{}

PlayerStateAttacking::~PlayerStateAttacking(void){}

void PlayerStateAttacking::activate()
{
	_player->setFlipChangeAllow(false);
	_attackIncrement = 0;
	_player->getAnimationMgr().setAnimation("cut_1");
	_swing01SFX->play();
	_za = new ZeroAttack(_player, sf::FloatRect(0.0f, 0.0f, 32.0f, 32.0f), sf::Vector2f(30.0f * _player->getFlip(), 0.0f), _player->getNormalDamage());
	_entityManager->addEntity(_za);
}

void PlayerStateAttacking::deactivate()
{
	_player->setFlipChangeAllow(true);
	verifyMemAlloc();
}

int PlayerStateAttacking::update()
{
	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;

	if (_attackIncrement == 0)
		{
			if (_player->getAnimationMgr().getCurrentID() == "cut_1")
			{
				if (_player->getAnimationMgr().getCurrentFrame() >= 3 && _player->getControlState()->isKeyPressed(CControls::Key(CTRL_SHOOT, 0), 0))
				{
					verifyMemAlloc();
					_za = new ZeroAttack(_player, sf::FloatRect(0.0f, 0.0f, 84.0f, 16.0f), sf::Vector2f(10.0f * _player->getFlip(), 0.0f), _player->getNormalDamage());
					_entityManager->addEntity(_za);
					_swing02SFX->play();
					_attackIncrement++;
					_player->getAnimationMgr().setAnimation("cut_2");
				}
				else if (_player->getAnimationMgr().isDone())
				{
					_attackIncrement = 0;
					if (_player->getMovementStateX() != MSX_NONE)
						return PSRV_MOVING_GROUND;
					else
						return PSRV_IDLE;
				}
			}
		}
		else if (_attackIncrement == 1)
		{
			if (_player->getAnimationMgr().getCurrentID() == "cut_2")
			{
				if (_player->getAnimationMgr().getCurrentFrame() >= 3 && _player->getControlState()->isKeyPressed(CControls::Key(CTRL_SHOOT, 0), 0))
				{
					verifyMemAlloc();
					_za = new ZeroAttack(_player, sf::FloatRect(0.0f, 0.0f, 46.0f, 46.0f), sf::Vector2f(30.0f * _player->getFlip(), 0.0f), _player->getNormalDamage());
					_entityManager->addEntity(_za);
					_swing03SFX->play();
					_attackIncrement++;
					_player->getAnimationMgr().setAnimation("cut_3");
				}
				else if (_player->getAnimationMgr().isDone())
				{
					_attackIncrement = 0;
					if (_player->getMovementStateX() != MSX_NONE)
						return PSRV_MOVING_GROUND;
					else
						return PSRV_IDLE;
				}
			}
		}
		else if (_attackIncrement == 2)
		{
			if (_player->getAnimationMgr().getCurrentID() == "cut_3")
			{
				if (_player->getAnimationMgr().isDone())
				{
					_attackIncrement = 0;
					if (_player->getMovementStateX() != MSX_NONE)
						return PSRV_MOVING_GROUND;
					else
						return PSRV_IDLE;
				}
			}
		}
	return PSRV_NOTHING;
}

void PlayerStateAttacking::verifyMemAlloc()
{
	if (_za != nullptr)
		_za->kill();
}

